package com.dru.drutils.components;

import android.content.Context;
import android.view.View;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;

import com.dru.drutils.media.RemoteImage;
import com.dru.drutils.media.RemoteImageEventListener;

public class RemoteImageView extends RelativeLayout implements RemoteImageEventListener {

	/**
	 * The remote image that backs this ImageView
	 */
	RemoteImage remoteImage = null;
	ImageView imageView = null;
	ProgressBar progressWheel = null;
	
	/**
	 * Constructs a new RemoteImageView backed by a RemoteImage
	 * that will load the image at imageUrl
	 * @param context the context of this View
	 * @param imageUrl the URL of the image to load into this ImageView
	 */
	public RemoteImageView(Context context, String imageUrl) 
	{
		super(context);
		
		remoteImage = new RemoteImage(imageUrl);

		this.initializeComponents(context);
	}
	
	/**
	 * Constructs a new RemoteImageView backed by a RemoteImage
	 * that will load the image at imageUrl
	 * @param context the context of this View
	 * @param remoteImage the RemoteImage to back this ImageView
	 */
	public RemoteImageView(Context context, RemoteImage remoteImage) 
	{
		super(context);
		
		this.remoteImage = remoteImage;
		
		this.initializeComponents(context);
		
		// if downloaded, set image
		if (this.remoteImage.isDownloaded())
		{
			this.onImageDownloaded();
			return;
		}
		
		// if its not downloading (and not already downloaded, above), begin downloading
		if (!this.remoteImage.isDownloading())
		{
			this.remoteImage.beginDownloadingImage();
		}
	}

	private void initializeComponents(Context context) {
		this.remoteImage.setEventHandler(this);

		this.imageView = new ImageView(context);
		
		this.progressWheel = new ProgressBar(context);
		
		this.remoteImage.setEventHandler(this);
		
		this.remoteImage.beginDownloadingImage();
		
		this.progressWheel.setIndeterminate(true);
		
		this.addView(this.progressWheel);

		this.addView(this.imageView);

		LayoutParams layoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
		layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT);
		
		this.progressWheel.setLayoutParams(layoutParams);
		
		this.imageView.setLayoutParams(layoutParams);
		
		this.imageView.setScaleType( ImageView.ScaleType.CENTER );
		
		this.imageView.setVisibility(View.GONE);
	}

	/**
	 * Called when the RemoteImage has finished downloading,
	 * sets the ImageView to hold the downloaded image
	 */
	public void onImageDownloaded() {
		imageView.setImageBitmap(remoteImage.getBitmap());
		
		this.progressWheel.setVisibility(View.GONE);
		
		this.imageView.setVisibility(View.VISIBLE);
		
		this.invalidate();
	}

	/**
	 * Called when the image can't be downloaded, should display something that conveys that to the user
	 */
	public void onImageCannotBeDownloaded() {
		// show a cannot be downloaded image or something;
	}

	/**
	 * Gets the RemoteImage backing this view
	 * @return the RemoteImage that backs this View
	 */
	public RemoteImage getRemoteImage() {
		return remoteImage;
	}

	/**
	 * Sets the RemoteImage backing this View, ties them together
	 * and begins loading the remote image
	 * @param image
	 */
	public void setRemoteImage(RemoteImage image) {
		this.remoteImage = image;
		if (image != null)
		{
			image.setEventHandler(this);
			
			if (!image.isDownloaded() && !image.isDownloading())
			{
				image.beginDownloadingImage();
			}
		}
	}
}
